package cn.bluethink.eguan.statistics.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.bluethink.eguan.core.security.Token;
import cn.bluethink.eguan.core.service.EgUserService;
import cn.bluethink.eguan.model.core.EgPurview;
import cn.bluethink.eguan.model.core.EgRole;
import cn.bluethink.eguan.model.core.EgUser;
import cn.bluethink.eguan.model.okrtask.EgOkrTask;
import cn.bluethink.eguan.model.okrtask.EgTarget;
import cn.bluethink.eguan.result.PageInfo;
import cn.bluethink.eguan.result.ResponseMessage;
import cn.bluethink.eguan.result.ResponseResult;
import cn.bluethink.eguan.statistics.filter.OKRTaskFilter;
import cn.bluethink.eguan.statistics.service.StatisticService;

/**
 * @title 天眼模块 - 统计
 * @Copyright 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.1
 *
 * @note 修订历史： （1）liuxn于2018年10月29日构建初始版本0.1
 */
@CrossOrigin
@RestController
@RequestMapping("/eguan/v1.0/statistics")
public class StatisticController {

	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private EgUserService userService;
	
	@Autowired
	private StatisticService staticService;
	
	/**
	 * 目标规划与统计
	 * @return
	 * @throws Exception
	 */
	@Token
	@PostMapping("/taskplanning")
	public ResponseResult<PageInfo<EgOkrTask>> targetplanning(@RequestBody OKRTaskFilter filter) throws Exception {
		Long uid = (Long) request.getAttribute("userId");
		EgUser user = userService.getUser(uid, false);
		EgRole role = userService.mypurview(uid);
		if((role.getPurview() & EgPurview.STATISTIC_VW) == EgPurview.STATISTIC_VW) user = null;
		return new ResponseResult<>(ResponseMessage.OK, staticService.taskTarget(filter, user));
	}
	
	/**
	 * 目标跟踪展示
	 * @param filter
	 * @return
	 * @throws Exception	
	 */
	@Token
	@PostMapping("/tracktarget")
	public ResponseResult<PageInfo<EgTarget>> tracktarget(@RequestBody OKRTaskFilter filter) throws Exception {
		return new ResponseResult<>(ResponseMessage.OK, staticService.tracktarget(filter));
	}
	
	/**
	 * 查询任务目标上下级关联任务目标
	 * @param filter
	 * @return
	 * @throws Exception
	 */
	@Token
	@GetMapping("/relatetargets")
	public ResponseResult<List<Long>> relatetargets(@RequestParam("tid") Long tid) throws Exception{
		if(tid == null || tid == 0) return new ResponseResult<>(ResponseMessage.NOTNULL_ID);
		return new ResponseResult<>(ResponseMessage.OK, staticService.relatetargets(tid));
	}
	
}
